package com.example.algorithm.backtrcaking;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/8/12
 * 电话号码的字母组合
 */
public class PhoneNumCombination {

    List<String> list=new ArrayList<>();
    StringBuilder sb=new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if(digits==""||digits.length()==0){
            return list;
        }
        String []phoneNum={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
        backTrack(digits,phoneNum,0);
        return list;
    }

    private void backTrack(String digits, String[] phoneNum, int count) {
        if(count==digits.length()){
            list.add(sb.toString());
            return;
        }
        String str=phoneNum[digits.charAt(count)-'0'];
        int len=str.length();
        for(int i=0;i<len;i++)
        {
            sb.append(str.charAt(i));
            backTrack(digits,phoneNum,count+1);
            sb.deleteCharAt(sb.length()-1);
        }
    }

    public static void main(String[] args) {
        PhoneNumCombination phoneNumCombination = new PhoneNumCombination();
        List<String> list = phoneNumCombination.letterCombinations("23");
        list.stream().forEach(System.out::println);
    }
}
